package homework6;

import java.util.Vector;

import roborl.Action;
import roborl.Rewards;
import roborl.Robot;
import roborl.RobotState;
import roborl.Sensor;

public class SimpleRobo implements RobotState, Robot, Rewards, Sensor {

	public static final Action left = new Action("left");
	public static final Action right = new Action("right");
	public static final Action up = new Action("up");
	public static final Action down = new Action("down");

	public static final Vector<Action> as;
	static {
		as = new Vector<Action>();
		as.add(left);
		as.add(right);
		as.add(up);
		as.add(down);
	}

	private int x, y;
	private int bx, by;

	public SimpleRobo(int bx, int by) {
		this.bx = bx;
		this.by = by;
		x = 0;
		y = 0;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	@Override
	public Double getState(String meta) {
		// TODO Auto-generated method stub
		if (meta.equalsIgnoreCase("x"))
			return (double) x;
		else
			return (double) y;
	}

	static final Vector<String> meta = new Vector<String>();
	{
		meta.add("x");
		meta.add("y");
	}

	@Override
	public Vector<String> getStateMeta() {
		// TODO Auto-generated method stub
		return meta;
	}

	@Override
	public Vector<Double> getStates() {
		// TODO Auto-generated method stub
		Vector<Double> d = new Vector<Double>();
		d.add((double) x);
		d.add((double) y);
		return d;
	}

	public boolean equals(Object o) {
		if (!(o instanceof SimpleRobo))
			return false;
		if (((SimpleRobo) o).getStates().equals(getStates()))
			return true;
		return false;
	}

	@Override
	public RobotState getState() {
		// TODO Auto-generated method stub
		return this;
	}

	@Override
	public void takeAction(Action action) {
		// TODO Auto-generated method stub
		if (action == up) {
			y--;
			if (y < 0){
				y = 0;
			}
		} else if (action == down) {
			y++;
			if (y >= by)
				y = by - 1;

		} else if (action == left) {
			x--;
			if (x < 0)
				x = 0;
		} else if (action == right) {
			x++;
			if (x >= bx)
				x = bx - 1;
		} else
			throw new RuntimeException("Unkown action:" + action);
	}

	@Override
	public double getReward(RobotState state1,RobotState state) {
		// TODO Auto-generated method stub
		SimpleRobo robo = (SimpleRobo) state;
		if (robo.x == bx - 1 && robo.y == by - 1)
			return 100;
		return 0;
	}

	@Override
	public RobotState getState(Robot robot) {
		// TODO Auto-generated method stub
		SimpleRobo stat = new SimpleRobo(bx, by);
		stat.x = ((SimpleRobo) robot).x;
		stat.y = ((SimpleRobo) robot).y;
		return stat;
	}

	@Override
	public Vector<Double> getHiddenStates() {
		// TODO Auto-generated method stub
		return null;
	}
}
